<?php

Raise::load('core.RaiseCollection');
Raise::load('core.RaiseKeyValuePair');
Raise::load('core.RaiseVariable');

/**
 * RaiseDomTreeNodeCollection class
 * a collection of RaiseDomTreeNode
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.Collection
 * @since 1.2
 */
class RaiseDomTreeNodeCollection extends RaiseCollection {

    /**
     * Create a new RaiseDomTreeNodeCollection
     * @param array|RaiseCollection $a
     */
    function  __construct($a = array()) {
        parent::__construct($a);
    }

    /**
     * Add a RaiseDomTreeNode
     */
    public function add() {
        $a = func_get_args();
        if(count($a) == 2)
        {
            $this->validateValues($a[1]);
            parent::add($a[0], $a[1]);
        }
        elseif(count($a) == 1)
        {
            if($a[0] instanceof RaiseKeyValuePair){
                $this->validateValues($a[0]->value());
                parent::add($a[0]);
            }else{
                $this->validateValues($a[0]);
                parent::add($a[0]);
            }
        }
        else
        {
            throw new RaiseInvalidArgumentException('The argument count is invalid. Should be add($key, $value) or add($value)');
        }
    }

    /**
     * Validate the value and throw the appropriate exception if an error is detected
     * @param RaiseUIControl $o
     */
    private function validateValues($o){
        if(!$o){
            throw new RaiseNullArgumentException('$value is null and cannot be added as a RaiseDomTreeNode to the RaiseUIControlCollection');
        }
        if(!($o instanceof RaiseDomTreeNode) && !RaiseVariable::isType($o, RaiseVariable::TYPESTRING)){
            throw new RaiseInvalidArgumentException('$value must be an object of RaiseDomTreeNode or string.');
        }
    }

    /**
     * Get a particualr RaiseDomTreeNode from the collection of nodes
     * @param string|integer $key The key to fetch
     * @return RaiseDomTreeNode|string
     */
    public function get($key) {
        return parent::get($key);
    }

}